package logic

import (
	"context"
	"fmt"
	"math/rand"
	"time"
	"user-sys/models"
	"user-sys/user_rpc/internal/svc"
	"user-sys/user_rpc/user_rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type SmsCaptchaLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewSmsCaptchaLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SmsCaptchaLogic {
	return &SmsCaptchaLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *SmsCaptchaLogic) SmsCaptcha(in *user_rpc.SmsCaptchaRequest) (*user_rpc.SmsCaptchaResponse, error) {
	// todo: add your logic here and delete this line
	key := fmt.Sprintf("phone from SmsCaptcha:%s", in.Phone)
	code := fmt.Sprintf("%d", rand.Intn(9000)+1000)
	//captcha, err := pkg.SmsCaptcha(code)
	//if err != nil {
	//	return nil, fmt.Errorf("短信发送失败")
	//}
	//if *captcha.Body.Code != "OK" {
	//	return nil, fmt.Errorf("短信发送失败")
	//}
	models.RDB.Set(models.Ctx, key, code, 5*time.Minute)
	return &user_rpc.SmsCaptchaResponse{}, nil
}
